﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BAL.Entities;
using BAL.Utils;
using BAL.Components;

namespace DigitalAnts.User
{
    public partial class FavoriteList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.gvProductList.DataSource = UserFavoriteList();
                this.gvProductList.DataBind();

            }
        }

        public DataTable UserFavoriteList()
        {
            DataTable dt = null;
            clsProduct obj = new clsProduct();
            List<int> data = (List<int>)Session["Favorite"];
            if (data.Count > 0)
            {
                obj.ListProductID = clsCartAndFavorite.getStringID(data);
                dt = clsProductBAL.doGetCartOrFavoriteList(obj);
            }
            return dt;
        }

        public void doUpdateCart(string cart)
        {
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    clsUser obj = new clsUser();
                    obj.UserID = Convert.ToInt32(Session["UserID"]);
                    obj.Card = cart;
                    clsUserBAL.doUpdateUserCart(obj);
                }
            }
        }

        protected void gvProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productID = Int32.Parse(gvProductList.DataKeys[e.RowIndex].Value.ToString());
            List<int> cart = (List<int>)Session["Favorite"];
            cart = clsCartAndFavorite.deleteAnID(cart, productID);
            Session["Favorite"] = cart;
            doUpdateCart(clsCartAndFavorite.getStringID(cart));
            Response.Redirect(Request.Url.ToString());
        }
    }
}